/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.GLAccountDAO;
import com.ivanbiz.dao.impl.GLAccountDAOImpl;
import com.ivanbiz.model.GLAccount;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class AccountService extends SelectorComposer<Component> {

    GLAccountDAO gLAccountDAO;
    @Wire
    Listbox listAccount;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        gLAccountDAO = new GLAccountDAOImpl();
        loadAccount();
    }

    private void loadAccount() {
        try {
            listAccount.getItems().clear();
            int no = 0;
            int x = 0;
            List<GLAccount> loadAccount = gLAccountDAO.getAll(GLAccount.class);
            for (GLAccount gLAccount : loadAccount) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(gLAccount);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(gLAccount.getGroupACC()));
                item.appendChild(new Listcell(gLAccount.getKode()));
                item.appendChild(new Listcell(gLAccount.getNama()));
                item.appendChild(new Listcell(gLAccount.getNoGL()));
                item.appendChild(new Listcell(gLAccount.getNameGL()));
                item.appendChild(new Listcell(gLAccount.getDeskripsi()));
                listAccount.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#tambah;")
    public void tambah() {
        Window win = (Window) Executions.createComponents("/accountAction.zul", null, null);
        win.doModal();
        loadAccount();
    }

    @Listen("onClick=#ubah;")
    public void ubah() {
        if (listAccount.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di ubah");
        } else {
            GLAccount gLAccount = listAccount.getSelectedItem().getValue();
            Map map = new HashMap();
            map.put("account", gLAccount);
            Window win = (Window) Executions.createComponents("/accountAction.zul", null, map);
            win.setTitle("Ubah Account");
            win.doModal();
            loadAccount();
        }
    }

    @Listen("onClick=#hapus;")
    public void hapus() throws Exception {
        if (listAccount.getSelectedItem() == null) {
            Messagebox.show("Pilih data yang akan di hapus");
        } else {
            GLAccount gLAccount = listAccount.getSelectedItem().getValue();
            gLAccountDAO.delete(gLAccount);
            loadAccount();
        }
    }
}
